import { defineStore } from 'pinia'
import useDynamicRouterStore from '@/store/dynamic-router.js'
import { removeRouteListener } from '@/utils/router-listener.js'
import { getUserInfo, removeToken, removeUserInfo, setToken, setUserInfo } from '@/utils/storage.js'
import { adminLogin } from "@/api/index.js"
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'

const useUserInfoStore = defineStore('userInfo', {
    state: () => ({
        userInfo: getUserInfo()
    }),
    actions: {
        updateUserInfo(data) {
            this.userInfo = data
            setUserInfo(data)
        },
        login(data) {
            const router = useRouter()
            adminLogin(data).then(res => {

                if (res.code === 200) {

                    this.userInfo = res.data.admin

                    setUserInfo(res.data.admin)
                    setToken(res.data.token)

                }
            })



        },
        logout() {
            const dynamicRouter = useDynamicRouterStore()

            removeToken()
            this.userInfo = {}
            removeUserInfo()
            removeRouteListener()
            dynamicRouter.syncRoutes = false
            dynamicRouter.roleRoutes = []
        }
    }
})

export default useUserInfoStore